<template>
  <div class="list">
    <pannle title="管理员信息">
     <div v-if="userInfo">
          <p>管理员ID： {{userInfo.id}} </p>
      <el-divider></el-divider>
      <p>账号： {{userInfo.account}}</p>
      <el-divider></el-divider>
      <p>用户组： {{userInfo.userGroup}}</p>
      <el-divider></el-divider>
      <p>创建时间： {{userInfo.ctime}}</p>
      <el-divider></el-divider>
      <p>
        管理员头像
        <el-upload
          class="avatar-uploader"
          :action="'http://localhost:5000/users/avatar_upload?id='+userInfo.id"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </p>
     </div>
    </pannle>
  </div>
</template>

<script>
import Pannle from "@/components/Pannle.vue";
import { accountInfo } from "../../api/user";
export default {
  data() {
    return {
        baseUrl:"http://localhost:5000",
      imageUrl: "",
      userInfo: null,
    };
  },
  components: {
    Pannle,
  },
  methods: {
    // 成功的回调头像
    handleAvatarSuccess(res, file, fileList) {
        this.imageUrl=this.baseUrl+res.imgUrl;
        // 中央事件总线 
        this.$bus.$emit('touxiang')
    },
    // 上传之前的回调
    beforeAvatarUpload(file) {},
    async getuserInfo() {
      let { id } = JSON.parse(localStorage.getItem("user"));
      let res = await accountInfo({id});
      this.userInfo = res.data.accountInfo;
      this.imageUrl=res.data.accountInfo.imgUrl;
    },
  },
  created() {
    // 请求用户信息
    this.getuserInfo();
  },
};
</script>

<style lang="less" scoped>
.list {
  width: 100%;
  height: 100%;
}
p {
  font-weight: bold;
  font-size: 1.3rem;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>